<?php

namespace common\service;

use common\model\ReviewRequestNodeModel;
use common\model\VersionCheckListModel;
use common\model\ReviewRequestModel;
use common\model\SettlementModel;
use common\model\VersionModel;
use common\model\TaskModel;
use common\model\UserModel;
use Exception;
use support\SingletonTrait;
use think\Hook;
use think\module\Module;
use Webman\Stomp\Client;

/**
 * 审核申请通知服务
 */
class ReviewRequestMessageService
{
    use SingletonTrait;

    /**
     * 查询计划待确认消息模板id
     * @return int
     */
    public function planWaitConfirmMessageTemplateId()
    {
        $messageTemplateId = OptionsService::getInstance()->getOptionsConfigItemData('message_notify_template_config', 'plan_wait_confirm_message_template_id', 57);
        return (int)$messageTemplateId;
    }

    /**
     * 通知下一个审核人
     * @param $nextChild
     * @throws Exception
     */
    public function notifyReviewNextNode($nextChild)
    {
        $reviewRequest = model(ReviewRequestModel::class)->find($nextChild['review_request_id']);
        $moduleId = $reviewRequest['module_id'];
        $module = Module::$moduleDictData['module_index_by_id'][$moduleId];
        switch ($module['code']) {
            case "version":
                $version = model(VersionModel::class)->find($reviewRequest['link_id']);
                $versionModule = Module::$moduleDictData['module_index_by_id'][$version['module_id']];
                switch ($versionModule['code']) {
                    case "task":

                        /**
                         *
                         *
                         * 内外部通知消息不一样
                         * 需要判断甲乙双方
                         *
                         *
                         */

                        $taskService = TaskService::getInstance();
                        $tenantId = request()->getTenantId();
                        $parentTask = $taskService->getParentTask($version['link_id'], $version['project_id']);
                        if ($parentTask && $parentTask['type'] == "shadow" && $tenantId != $parentTask['tenant_id']) {
                            //外部任务审核
                            $hookData = [
                                'type' => 'commit_version',
                                'data' => [
                                    'version_id' => $version['id'],
                                    'review_request_id' => $reviewRequest['id'],
                                    'receiver' => [
                                        [
                                            'user_id' => $nextChild['plan_reviewer'],
                                            'role' => 'plan_reviewer',
                                        ]
                                    ],
                                ],
                            ];
                            Hook::listen('task_review', $hookData);
                            $hookData = [
                                'type' => 'task_review_outsource',
                                'data' => [
                                    'version' => $version,
                                    'next_child' => $nextChild,
                                    'review_request' => $reviewRequest,
                                    'parent_task' => $parentTask,
                                ],
                            ];
                            Hook::listen('task_review_created_after', $hookData);
                        } else {
                            // 内部任务审核
                            $hookData = [
                                'type' => 'commit_version',
                                'data' => [
                                    'version_id' => $version['id'],
                                    'review_request_id' => $reviewRequest['id'],
                                    'receiver' => [
                                        [
                                            'user_id' => $nextChild['plan_reviewer'],
                                            'role' => 'plan_reviewer',
                                        ]
                                    ],
                                ],
                            ];
                            Hook::listen('task_review', $hookData);
                            $hookData = [
                                'type' => 'task_review',
                                'data' => [
                                    'version' => $version,
                                    'next_child' => $nextChild,
                                    'review_request' => $reviewRequest,
                                ],
                            ];
                            Hook::listen('task_review_created_after', $hookData);
                        }
                        break;
                    default:
                }
                break;
            case "settlement":
                $financeService = new FinanceService();
                $settlement = model(SettlementModel::class)->find($reviewRequest['link_id']);
                $financeService->notifyReviewSettlement($settlement, $nextChild);
                break;
            case "task":
                break;
            case "order":
            default:
        }
    }

    /**
     * 通知测试
     * @param int $nodeId
     * @throws Exception
     */
    public function notifyNextChild(int $nodeId)
    {
        $child = model(ReviewRequestNodeModel::class)->find($nodeId);
        $this->notifyReviewNextNode($child);
    }


    /**
     * 排期确认结果消息模板id
     * @return int
     */
    public function planConfirmResultMessageTemplateId()
    {
        $messageTemplateId = OptionsService::getInstance()->getOptionsConfigItemData('message_notify_template_config', 'plan_confirm_result_message_template_id', 60);
        return (int)$messageTemplateId;
    }

    /**
     * 查询任务延期确认消息模板id
     * @return int
     */
    public function taskDelayConfirmMessageTemplateId()
    {
        $messageTemplateId = OptionsService::getInstance()->getOptionsConfigItemData('message_notify_template_config', 'task_delay_confirm_message_template_id', 58);
        return (int)$messageTemplateId;
    }

    /**
     * 任务延期审核结果通知模板id
     * @return int
     */
    public function taskDelayResultMessageTemplateId()
    {
        $messageTemplateId = OptionsService::getInstance()->getOptionsConfigItemData('message_notify_template_config', 'task_delay_result_message_template_id', 59);
        return (int)$messageTemplateId;
    }

    /**
     * 查询任务延期确认消息模板id
     * @return int
     */
    public function delayOrderConfirmMessageTemplateId()
    {
        $messageTemplateId = OptionsService::getInstance()->getOptionsConfigItemData('message_notify_template_config', 'delay_order_confirm_message_template_id', 61);
        return (int)$messageTemplateId;
    }

    /**
     * 延期确认消息模板id
     * @return int
     */
    public function delayOrderResultMessageTemplateId()
    {
        $messageTemplateId = OptionsService::getInstance()->getOptionsConfigItemData('message_notify_template_config', 'delay_order_result_message_template_id', 62);
        return (int)$messageTemplateId;
    }

}
